import { LOGIN_START,LOGIN_SUCCESS,LOGIN_FAIL } from "../action/actionTypes";

let initData = {
    userInfo:null,
    isLoading:false
}
export default function(state=initData,action){
    switch (action.type) {
        case LOGIN_START:
            state.isLoading = true
            return {...state}
        case LOGIN_SUCCESS:
            state.isLoading = false
            state.userInfo = action.payload
            let saveData = JSON.stringify(action.payload)
            action.payload.remember 
            ? localStorage.setItem('userInfo',saveData)
            : sessionStorage.setItem('userInfo',saveData)
            return {...state}   
        case LOGIN_FAIL:
            state.isLoading = false
            state.userInfo = null
            localStorage.removeItem('userInfo')
            sessionStorage.removeItem('userInfo')
            return {...state} 
        default:
            return state
    }
}